\insert Lista.oz
\insert Visualizer.oz

declare 
fun {AplicarMovimiento E Ms}
   	case Ms of nil then [E]
	[] M|Mr then
		local E1 ElemPrincipal ElemUno ElemDos Long in
			E1 = case M of one(N) then
				if(N > 0) then
					Long = {Longitud E.main}
					if(Long =< N) then
						ElemPrincipal = {Tomar E.main N}
						{Eliminar E.main N ElemUno}
						E1 = estado(main: nil  one:
							{Agregar ElemPrincipal E.one} two: E.two)		    
					else   		      
						ElemPrincipal = {Tomar E.main Long-N}
						{Eliminar E.main Long-N ElemUno}
						E1 = estado(main: ElemPrincipal one: {Agregar ElemUno E.one} two: E.two)
					end
				elseif (N < 0) then
					local Nabs Neg in
						Neg = ~1
						{Number.'*' N Neg Nabs}
						Long = {Longitud E.one}
						if(Long =< Nabs) then
							ElemPrincipal = {Tomar E.one Nabs}
							{Eliminar E.one Nabs ElemUno}
							E1 = estado(main: ElemPrincipal  one:
								nil two: E.two)
						else
							ElemPrincipal = {Tomar E.one Long-Nabs}
							{Eliminar E.one Long-Nabs ElemUno}
							E1 = estado(main: {Agregar E.main ElemUno} one: ElemPrincipal two: E.two)
						end
					end
				end
				
				[] two(N) then
				if(N > 0) then
					Long = {Longitud E.main}
					if(Long =< N) then
						ElemPrincipal = {Tomar E.main N}
						{Eliminar E.main N ElemDos}
						E1 = estado(main: nil  one:
							E.one two: {Agregar ElemPrincipal E.two})		    
					else   		      
						ElemPrincipal = {Tomar E.main Long-N}
						{Eliminar E.main Long-N ElemDos}
						E1 = estado(main: ElemPrincipal one: E.one two: {Agregar ElemDos E.two})
					end
				elseif (N < 0) then
					local Nabs Neg in
						Neg = ~1
						{Number.'*' N Neg Nabs}
						Long = {Longitud E.two}
						if(Long =< Nabs) then
							ElemPrincipal = {Tomar E.two Nabs}
							{Eliminar E.two Nabs ElemDos}
							E1 = estado(main: ElemPrincipal  one:
								E.one two: nil)
						else
							ElemPrincipal = {Tomar E.two Long-Nabs}
							{Eliminar E.two Long-Nabs ElemDos}
							E1 = estado(main: {Agregar E.main ElemDos} one: E.one two: ElemPrincipal)
						end
					end
				end
				
			else
				local A in
					A = 21
				end
			end
			E|{AplicarMovimiento E1 Mr}
		end
	end
end

{Visualize {AplicarMovimiento estado(main:[a b c d e f] one: nil two: nil) [one(2) one(1) two(1) two(1) one(1)]}}
